package com.pts.servlet.product;


import com.pts.dao.Dao;
import com.pts.dao.EnterpriseDao;
import com.pts.dao.ProduceDao;
import com.pts.dao.ProductDao;
import com.pts.vo.Enterprise;
import com.pts.vo.Produce;
import com.pts.vo.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//生产信息溯源
@WebServlet(value = "/prolist")
public class SelectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        ProductDao dao = new ProductDao();
        String card = request.getParameter("card");
        System.out.println(card);
        Product product = dao.findByCard(card);
        if(product==null){
            response.sendRedirect("admin/product/Error.jsp");
            return;
        }
        Dao produceDao = new ProduceDao();
        Produce produce = (Produce) produceDao.findById(product.getPid());
        if(produce==null){
            response.sendRedirect("admin/product/Error.jsp");
            return;
        }
        String typeName = ((ProduceDao)produceDao).selectTypeName(product.getTid());
        String stateName = ((ProduceDao)produceDao).selectStateName(product.getState());
        Dao enterpriseDao = new EnterpriseDao();
        Enterprise enterprise= (Enterprise) enterpriseDao.findById(produce.getEid());
        if(enterprise==null){
            response.sendRedirect("admin/product/Error.jsp");
            return;
        }
        request.setAttribute("produce",produce);
        request.setAttribute("product",product);
        request.setAttribute("enterprise",enterprise);
        request.setAttribute("state",stateName);
        request.setAttribute("type",typeName);
        request.getRequestDispatcher("admin/product/Yes.jsp").forward(request,response);
    }
}
